import sqlite3

import streamlit as st
import base64

# 处理本地图片并设置为背景
def set_local_background(image_path):
    # 读取本地图片并进行Base64编码
    with open(image_path, "rb") as image_file:
        encoded_string = base64.b64encode(image_file.read()).decode()

    # 注入CSS样式，使用编码后的图片作为背景
    st.markdown(
        f"""
        <style>
        .stApp {{
            background-image: url("data:image/jpeg;base64,{encoded_string}");
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            background-attachment: fixed;
        }}
        /* 可选：调整页面元素背景透明度，避免遮挡背景图 */
        .stBlockContainer {{
            background-color: rgba(255, 255, 255, 0.7);  /* 白色半透明背景 */
            padding: 2rem;
            border-radius: 10px;
        }}
        </style>
        """,
        unsafe_allow_html=True
    )



set_local_background("C:/Users/27571/Desktop/机器视觉/aisaka-taiga/apex查询/back.jpg")  # 支持jpg、png等格式






st.markdown("<h1 style='color: white; text-shadow: 2px 2px 5px black;'> 创建账号</h1>", unsafe_allow_html=True)

# 输入框
username2 = st.text_input("请输入用户名", key="register_username")
password2 = st.text_input("请输入密码", type="password", key="register_password")
password3 = st.text_input("请确认密码", type="password", key="register_confirm_password")


def register_user(username2, password2):
    conn = sqlite3.connect('taobao.db')
    cursor = conn.cursor()
    try:
        cursor.execute('INSERT INTO users (name, password) VALUES (?, ?)', (username2, password2))
        conn.commit()
        return True
    except sqlite3.IntegrityError:
        return False
    finally:
        conn.close()

if st.button("注册"):
    if not username2 or not password2:
        st.error("请填写所有字段！")
    elif len(password2) < 6:
        st.error("密码长度不能少于6位！")
    elif password2 != password3:
        st.error("两次输入的密码不一致！")
    else:
        if register_user(username2, password2):
            st.success(f" 账号 {username2} 创建成功！")
        else:
            st.error("用户名已存在！")